/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-24
 * Time: 18:21
 */
//恶汉方法的单例模型
class OneMode1{
    private int count=0;
    private static OneMode1 class1=new OneMode1();

    private OneMode1(){};

    public static OneMode1 getClass1() {
        return class1;
    }

    public int getCount(){
        return count;
    }

    public void fun(){
        synchronized(this){
            System.out.println("打印一个数"+count);
            count++;
        }
    }
}
//懒汉方法实线单例模型
class OneMode2{
    private static volatile OneMode2 class2=null;
    private static Object lock=new Object();
    private static int count=0;

    private OneMode2(){};

    public static OneMode2 getClass2() {
        if(class2==null){
            synchronized(lock) {
                if (class2 == null) {
                    class2 = new OneMode2();
                }
            }
        }
        return class2;
    }

    public void fun(){
        synchronized(this){
            System.out.println("打印一个数"+count);
            count++;
        }
    }
}
public class Test {
    static int a=0;
    public static void main(String[] args) throws InterruptedException  {
        Thread t1=new Thread(){
            @Override
            public void run() {
                OneMode2 o2=OneMode2.getClass2();
                for(int i=0;i<50000;i++){
                    o2.fun();
                }
            }
        };
        Thread t2=new Thread(){
            @Override
            public void run() {
                OneMode2 o2=OneMode2.getClass2();
                for(int i=0;i<50000;i++){
                    o2.fun();
                }
            }
        };
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
